/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 */

package src.clovece.game;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Class used for loading saved settings from XML and games
 */
public class Load {
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    private static Load load = new Load();
    private String playerNickname = new String();
    private int graphicMode;
    private int chosenFigurine;
    private int die;
    private int playerOnTurn;
    private int gameBoardSize;
    private int numberOfPlayers;
    private int [] generatedValues = new int[6];
    private boolean gamePhase;
    private Player[] players = null;


    private Load ()
    {
        super();

    }

    public static Load getLoadInstance() {
        return Load.load;
    }

    /**
     * Method used to load saved game
     */
    public void LoadGame (String savename)
    {

        Properties load = new Properties();

        try {
            FileInputStream in = new FileInputStream(System.getProperty("user.dir") + System.getProperty("file.separator") + "examples" +
                   System.getProperty("file.separator")  + "save" + System.getProperty("file.separator") + savename);
            load.load(in);
            
        }
            catch (IOException e) {

        }
        playerNickname = load.getProperty("PlayerNickName");
        graphicMode = Integer.parseInt(load.getProperty("GraphicMode"));
        chosenFigurine = Integer.parseInt(load.getProperty("ChosenFigurine"));
        die = Integer.parseInt(load.getProperty("Die"));
        playerOnTurn = Integer.parseInt(load.getProperty("PlayerOnTurn"));
        gameBoardSize = Integer.parseInt(load.getProperty("GameBoardSize"));
        numberOfPlayers = Integer.parseInt(load.getProperty("NumberOfPlayers"));

        if (load.getProperty("GamePhase").equals("true"))
            gamePhase = true;
        else gamePhase = false;

        for (int i = 0; i < 6; i++)
            generatedValues[i] = Integer.parseInt(load.getProperty("GeneratedValue"+i));

        players = new Player[numberOfPlayers];

        for (int i = 0; i < numberOfPlayers; i++)
        {
            if (i == 0)
            {
                players[i] = new Player(graphicMode, chosenFigurine, i, playerNickname);
            }
            else
            {
                if(i == chosenFigurine) {
                    players[i] = new AiPlayer(graphicMode, 0, i);
                }
                else {
                    players[i] = new AiPlayer(graphicMode, i, i);
                }
                AiPlayer cpu = (AiPlayer) players[i];
                if (load.getProperty("Player"+i+"-RandomMove").equals("true")) cpu.setRandomMove(true);
                if (load.getProperty("Player"+i+"-PreferHome").equals("true")) cpu.setPreferHome(true);
                if (load.getProperty("Player"+i+"-PreferNew").equals("true")) cpu.setPreferNew(true);

            }
        }

        for(int i = 0; i < this.numberOfPlayers; i++) {
            for(int j = 0; j < 4; j++) {
                this.players[i].getFigurines()[j].setActualPosition(Integer.parseInt(load.getProperty("Player"+i+"Figurine"+j+"-ActualPosition")));
                this.players[i].getFigurines()[j].setStartingPosition(Integer.parseInt(load.getProperty("Player"+i+"Figurine"+j+"-StartingPosition")));
                this.players[i].getFigurines()[j].setCounter(Integer.parseInt(load.getProperty("Player"+i+"Figurine"+j+"-Counter")));
                if(load.getProperty("Player"+i+"Figurine"+j+"-GameCompleted").equals("true")) this.players[i].getFigurines()[j].setGameCompleted(true);
            }
        }
        
    }
    /**
     *  Used for loading settings from XML file
     */
     public void LoadSettings()
     {
         Properties settings = new Properties();

        try {
            FileInputStream in = new FileInputStream(System.getProperty("user.dir") + System.getProperty("file.separator") + "lib" +
                   System.getProperty("file.separator") + "settings.xml");
            settings.loadFromXML(in);

        }
            catch (IOException e) {

        }

         gameInfo.setGraphicMode(Integer.parseInt(settings.getProperty("GameSet")));
         gameInfo.setHost(settings.getProperty("Host"));
         gameInfo.setNumberOfPlayers(Integer.parseInt(settings.getProperty("NumberOfPlayers")));
         gameInfo.setGameBoardSize(Integer.parseInt(settings.getProperty("GameBoardSize")));
         gameInfo.setPlayerNickname(settings.getProperty("Username"),0);
         gameInfo.setChosenFigurine(Integer.parseInt(settings.getProperty("ChosenFigurine")));
    }

     public int getNumberOfPlayers() {
         return this.numberOfPlayers;
     }

     public int getGameBoardSize() {
        return this.gameBoardSize;
     }

     public int getDie() {
         return this.die;
     }

     public int getPlayerOnTurn() {
        return this.playerOnTurn;
     }

     public boolean getGamePhase() {
        return this.gamePhase;
     }

     public int[] getGeneratedValues() {
        return this.generatedValues;
     }

     public String getPlayerNickname() {
         return this.playerNickname;
     }

     public Player[] getPlayers(){
         return this.players;
     }

     public int getChosenFigurine() {
        return this.chosenFigurine;
     }

     public int getGraphicMode() {
        return this.graphicMode;
     }
}

